package com.lxyk.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @ClassName MyMvcConfig
 * @Description TODO
 * @Author FJQ
 * @Date 2022/10/26 17:46
 * @Version 1.0
 **/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自己的拦截器,并设置拦截的请求路径
        //addPathPatterns为拦截此请求路径的请求
        //excludePathPatterns为不拦截此路径的请求
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")//拦截所有请求，包括静态资源文件
                .excludePathPatterns("/", "/login", "/index.html", "/css/**", "/img/**", "/js/**", "/fonts/**","favicon.ico");//放行登录页，登陆操作，静态资
    }

    //在配置类 MyMvcConfig 中添加视图映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //当访问 “/index111.html” 时，直接跳转到登陆页面
        registry.addViewController("/index.html").setViewName("index");
    }
}
